package com.example.scale;

import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.util.Log;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

public class Drone extends Sprite {

	public Body body;
	public float attackDistance = 400f;
	public float droneSpeed = 2f;
	
	public Drone(float pX, float pY, VertexBufferObjectManager pVertexBufferObjectManager) {
		super(pX, pY, ResourcesManager.getInstance().powerup_region, pVertexBufferObjectManager);
		body = PhysicsFactory.createBoxBody(GameScene.physicsWorld, this,
				BodyType.DynamicBody, PhysicsFactory.createFixtureDef(0, 0, .01f)); // Could be 0, .05, .02
		body.setUserData(this);
		GameScene.physicsWorld.registerPhysicsConnector(new PhysicsConnector(
				this, body, true, true) {
			@Override
			public void onUpdate(float pSecondsElapsed) {
				super.onUpdate(pSecondsElapsed);
				// TODO: 32 is PIXELS_PER_METER? Verify this
				float distanceX = 32*(GameScene.player.body.getPosition().x - body.getPosition().x);
				float distanceY = 32*(GameScene.player.body.getPosition().y - body.getPosition().y);
				float distanceToPlayer = (float) Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2));
				if(distanceToPlayer < attackDistance) {
					// Player is within drone's attack distance, start chasing
					body.setLinearVelocity((droneSpeed*distanceX/distanceToPlayer), (droneSpeed*distanceY/distanceToPlayer));
				} else {
					body.setLinearVelocity(0, .01f);
				}
			}
		});
	}

}
